﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using nms.gaming.rng;
using nms.gaming.rng.model;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;


namespace nms.gaming.webservice
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [JavascriptCallbackBehavior(UrlParameterName = "$callback")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    /// <summary>
    /// </summary>
    public class RandomNumber : IRandomNumberService
    {
        /// <summary>
        /// </summary>
        /// <param name="host"></param>
        /// <param name="game"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="length"></param>
        /// <param name="unique"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public RandomNumberSet[] Generate(string host, string game, int min, int max, int length, bool unique, int amount)
        {
            IRandomNumberGenerator _rng = new RandomNumberGenerator();
            List<RandomNumberSet> _response = _rng.GenerateRandomSet(min, max, length, unique, amount).ToList<RandomNumberSet>();
            RandomNumberSet[] array = _response.ToArray<RandomNumberSet>();

            audit(array);

            return array;
        }

        /// <summary>
        /// </summary>
        /// <param name="host"></param>
        /// <param name="game"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="length"></param>
        /// <param name="unique"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public RandomNumberSet[] GenerateJSON(string host, string game, int min, int max, int length, bool unique, int amount)
        {
            RandomNumberSet[] randomNumberSetArray = this.Generate(host, game, min, max, length, unique, amount);
            return randomNumberSetArray;
        }

        /// <summary>
        /// </summary>
        /// <param name="host"></param>
        /// <param name="game"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="length"></param>
        /// <param name="unique"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public RandomNumberSet[] GenerateJSON_EXPLICIT(string host, string game, int min, int max, int length, bool unique, int amount)
        {
            RandomNumberSet[] randomNumberSetArray = this.Generate(host, game, min, max, length, unique, amount);
            return randomNumberSetArray;
        }
    
        /// <summary>
        /// </summary>
        /// <param name="data"></param>
        private void audit(RandomNumberSet[] data)
        {
            Database _db = DatabaseFactory.CreateDatabase();
            using (DbConnection _dbConn = _db.CreateConnection())
            {
                /// -- open database connection
                _dbConn.Open();
                /// --
                try
                {
                    /// -- place gaming stake into database table
                    using (DbCommand _dbComm = _db.GetStoredProcCommand("sp_RNG_AUDIT"))
                    {

                        foreach (RandomNumberSet _set in data)
                        {
                            _db.SetParameterValue( _dbComm , "SET_INDEX" , _set.index );
                            foreach (nms.gaming.rng.model.RandomNumber _number in _set.values)
                            {
                                _db.SetParameterValue(_dbComm, "SET_NUMBER_INDEX", _number.Index);
                                _db.SetParameterValue(_dbComm, "SET_NUMBER_VALUE", _number.Value);
                                _db.ExecuteNonQuery(_dbComm);
                            }
                        }
                    }

                }
                catch
                { }
                finally 
                { }

                /// --
                _dbConn.Close();
            }
        }

    }
}
